#!/usr/bin/env python3

import numpy as np
import cv2 as cv

def on_trackbar(x):
    # pass
    print(x)

# 新建窗口
img = np.zeros((300,512,3), np.uint8)
cv.namedWindow("image")

# 创建轨迹栏
cv.createTrackbar('R', 'image', 0, 255, on_trackbar)
cv.createTrackbar('G', 'image', 0, 255, on_trackbar)
cv.createTrackbar('B', 'image', 0, 255, on_trackbar)

# 为 ON/OFF 功能创建开关
switch = '0:OFF\n1:ON'
cv.createTrackbar(switch, 'image', 0, 1, on_trackbar)

while True:
    cv.imshow('image', img)

    k = cv.waitKey(20) & 0XFF
    if k == 27:
        break

    # 获取四条轨迹的当前位置
    r = cv.getTrackbarPos("R", 'image')
    g = cv.getTrackbarPos("G", 'image')
    b = cv.getTrackbarPos("B", 'image')
    s = cv.getTrackbarPos(switch, 'image')

    # 根据开关状态设置图像颜色
    if s == 0:
        img[:] = 0
    else:
        img[:] = [b, g, r]

cv.destroyAllWindows()









